﻿using Omabar.Model;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Tom.Net;

namespace Omabar.Entity
{
    public class Trip_Entity : EntityBase<TRIP>
    {
        public override RESP_Info Add(TRIP model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "insert into TRIP(TRIP_ID,TRIP_NAME,TRIP_FOOD,TRIP_DES) values (@TRIP_ID,@TRIP_NAME,@TRIP_FOOD,@TRIP_DES)";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("TRIP_ID", model.TRIP_ID));
                    cmd.Parameters.Add(new SqlParameter("TRIP_NAME", model.TRIP_NAME));
                    cmd.Parameters.Add(new SqlParameter("TRIP_FOOD", model.TRIP_FOOD));
                    cmd.Parameters.Add(new SqlParameter("TRIP_DES", model.TRIP_DES));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "成功";
                    info.Tip = "<h5><p>添加行程成功</p></h5>";
                    info.Url = "TripList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>添加行程失败</p></h5>";
                    info.Url = "TripAdd.aspx";

                    Tom.Net.Log.LogManager.Error("添加行程失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Update(TRIP model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "update TRIP set TRIP_NAME=@TRIP_NAME,TRIP_FOOD=@TRIP_FOOD,TRIP_DES=@TRIP_DES where TRIP_ID=@TRIP_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("TRIP_ID", model.TRIP_ID));
                    cmd.Parameters.Add(new SqlParameter("TRIP_NAME", model.TRIP_NAME));
                    cmd.Parameters.Add(new SqlParameter("TRIP_FOOD", model.TRIP_FOOD));
                    cmd.Parameters.Add(new SqlParameter("TRIP_DES", model.TRIP_DES));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "编辑";
                    info.Tip = "<h5><p>编辑行程成功</p></h5>";
                    info.Url = "TripList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>编辑行程失败</p></h5>";
                    info.Url = "TripAdd.aspx";

                    Tom.Net.Log.LogManager.Error("编辑行程失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Delete(TRIP trip)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "delete from TRIP where TRIP_ID=@TRIP_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("TRIP_ID", trip.TRIP_ID));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "删除";
                    info.Tip = "<h5><p>删除行程成功</p></h5>";
                    info.Url = "TripList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>删除行程失败</p></h5>";
                    info.Url = "TripList.aspx";

                    Tom.Net.Log.LogManager.Error("删除行程失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override TRIP GetModel(string id)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "select TRIP_ID,TRIP_NAME,TRIP_FOOD,TRIP_DES from TRIP where TRIP_ID=@TRIP_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("TRIP_ID", id));

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        TRIP trip = new TRIP();
                        if (!reader.IsDBNull(0)) trip.TRIP_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) trip.TRIP_NAME = reader.GetString(1);
                        if (!reader.IsDBNull(2)) trip.TRIP_FOOD = reader.GetString(2);
                        if (!reader.IsDBNull(3)) trip.TRIP_DES = reader.GetString(3);
                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return trip;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取行程数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override TRIP GetModelBySQL(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        TRIP trip = new TRIP();
                        if (!reader.IsDBNull(0)) trip.TRIP_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) trip.TRIP_NAME = reader.GetString(1);
                        if (!reader.IsDBNull(2)) trip.TRIP_FOOD = reader.GetString(2);
                        if (!reader.IsDBNull(3)) trip.TRIP_DES = reader.GetString(3);

                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return trip;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取行程数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override List<TRIP> GetModels(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    List<TRIP> trips = new List<TRIP>();
                    while (reader.Read())
                    {
                        TRIP trip = new TRIP();
                        if (!reader.IsDBNull(0)) trip.TRIP_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) trip.TRIP_NAME = reader.GetString(1);
                        if (!reader.IsDBNull(2)) trip.TRIP_FOOD = reader.GetString(2);
                        if (!reader.IsDBNull(3)) trip.TRIP_DES = reader.GetString(3);

                        trips.Add(trip);
                    }
                    reader.Close();
                    reader.Dispose();
                    cmd.Dispose();

                    return trips;
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取行程数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return null;
        }
    }
}

